// pages/profile/profile.ts
import { axios } from "../../utils/request"
import Dialog from "@vant/weapp/dialog/dialog"
interface User {
  nickName: string
  avatarUrl: string
  gender: number
}
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo: <User | any>{},
    options: [
      { name: "微信", icon: "wechat", openType: "share" },
      { name: "微博", icon: "weibo" },
      { name: "复制链接", icon: "link" },
      { name: "分享海报", icon: "poster" },
      { name: "二维码", icon: "qrcode" },
    ],
    showShare: false,
    statusBarHeight: "",
    navBarHeight: "",
  },
  //获取用户code码
  login() {
    return new Promise((resolve, reject) => {
      wx.login({
        success(res) {
          resolve(res)
        },
        fail(err) {
          reject(err)
        },
      })
    })
  },
  //获取用户信息
  async getUserInfo() {
    try {
      const getInfo = () => wx.getUserProfile({ desc: "获取信息" })
      wx.showLoading({ title: "登陆中", mask: true })
      const [info, loginData] = await Promise.all([getInfo(), this.login()])
      let UserData = { code: (loginData as any).code, ...info.userInfo }
      let res = (await axios("/user", "POST", { ...UserData })) as Response
      const { token, userInfo } = res.data as any
      console.log(res)

      if (res.statusCode == 200) {
        wx.setStorageSync("token", token)
        wx.setStorageSync("userInfo", userInfo)
        wx.setStorageSync("isLogin", true)
        this.setData({
          userInfo: userInfo,
        })
      }
      setTimeout(function () {
        wx.hideLoading()
      }, 200)
      wx.showToast({ title: "登录成功", icon: "success" })
    } catch (error) {
      console.log(error)
      setTimeout(function () {
        wx.hideLoading()
      }, 200)
    }
  },
  //退出登录
  logout() {
    Dialog.confirm({
      title: "是否退出登录",
      message: "点击确认将退出登录",
      zIndex: 9999999,
    })
      .then(async () => {
        wx.removeStorageSync("userInfo")
        wx.removeStorageSync("token")
        wx.removeStorageSync("isLogin")
        this.setData({
          userInfo: "",
        })
      })
      .catch(() => {
        wx.showToast({
          title: "已取消",
          icon: "none",
        })
      })
  },
  editorInfo() {
    wx.navigateTo({
      url: "/pages/editor/editor",
    })
  },
  //分享
  share() {
    this.setData({ showShare: true })
  },
  onClose() {
    this.setData({ showShare: false })
  },
  //跳转到订单
  toOrder() {
    wx.navigateTo({
      url: "/pages/order/order",
    })
  },
  //跳转到收藏
  toStart() {
    wx.switchTab({ url: "/pages/start/start" })
  },
  //跳转到点餐
  toDinner() {
    wx.switchTab({ url: "/pages/dinner/dinner" })
  },
  toStrategy() {
    wx.navigateTo({
      url: "/pages/strategy/strategy",
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    wx.showLoading({
      title: "加载中",
      mask: true,
    })
    this.getTabBar().setData({
      active: 3,
    })

    const token = wx.getStorageSync("token")
    if (token) {
      const userInfo = wx.getStorageSync("userInfo")
      this.setData({
        userInfo,
      })
    }
    this.setData({
      statusBarHeight: wx.getStorageSync("statusBarHeight"),
      navBarHeight: wx.getStorageSync("navBarHeight"),
    })
    setTimeout(function () {
      wx.hideLoading()
    }, 200)
  },
})
